package sandbox;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;

public class Consumer {

	public static void main(String [] args) throws Exception {
		ConnectionFactory factory = new ConnectionFactory();
		factory.setUsername("guest");
		factory.setPassword("guest");
		factory.setVirtualHost("/");
		factory.setHost("192.168.1.130");
		factory.setPort(5672);
		Connection con = factory.newConnection();
		System.out.println(con);
		
		Channel c = con.createChannel();
		c.exchangeDeclare("myExchange1", "direct",true);
		c.queueDeclare("myQueue", true,false,false,null);
		c.queueBind("myQueue", "myExchange1", "testRoute");
		
		QueueingConsumer consumer = new QueueingConsumer(c);
		
		c.basicConsume("myQueue", false, consumer);
		while (true==true) {
			QueueingConsumer.Delivery delivery;
			try {
				delivery = consumer.nextDelivery();
				
			}
			catch (InterruptedException e) {
				continue;
			}
			System.out.println("Message Received "+new String(delivery.getBody()));
			c.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
		}
		
	}
}
